﻿using AiBLSmartEdu.Module.SmartDictionaries.API.Configurations;
using FrameworkCore.DI;
using Microsoft.Extensions.Options;
using Microsoft.International.Converters.TraditionalChineseToSimplifiedConverter;

namespace AiBLSmartEdu.Module.SmartDictionaries.API;

/// <summary>
/// 词典助手
/// </summary>
public partial class DictionariesHelpers : IScopedDependency
{
    private readonly IOptions<SmartDictionariesSettings> _optSmartDictionariesSettings;
    private readonly HttpClient _httpClient;

    public DictionariesHelpers(IOptions<SmartDictionariesSettings> optSmartDictionariesSettings, HttpClient httpClient)
    {
        _optSmartDictionariesSettings = optSmartDictionariesSettings;
        _httpClient = httpClient;
    }

    /// <summary>
    /// 获取词典
    /// </summary>
    /// <param name="word">词语</param>
    /// <returns></returns>
    public async Task<string> FetchWordDataFromSourceAsync(string word)
    {
        return await FetchWordDataAsync(word);
    }


    private async Task<string> FetchWordDataAsync(string word)
    {
        _httpClient.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3");
        _httpClient.DefaultRequestHeaders.Add("Accept", "application/json");

        try
        {
            string wordToTraditional = await ConvertToTraditionalAsync(word);
            string url = string.Format(_optSmartDictionariesSettings.Value.Url, wordToTraditional);

            HttpResponseMessage response = await _httpClient.GetAsync(url);
            response.EnsureSuccessStatusCode();

            string result = await response.Content.ReadAsStringAsync();
            string wordToSimplified = await ConvertToSimplifiedAsync(result);
            return wordToSimplified;
        }
        catch (HttpRequestException e)
        {
            return e.Message;
        }
    }

    /// <summary>
    /// 简繁体转换
    /// </summary>
    /// <param name="source">源文本</param>
    /// <param name="isToSimplified">转简体</param>
    /// <returns></returns>
    public async Task<string> ConvertChineseAsync(string source, bool isToSimplified = true)
    {
        if (isToSimplified)
        {
            return await ConvertToSimplifiedAsync(source);
        }
        else 
        {
            return await ConvertToTraditionalAsync(source);
        }
    }

    /// <summary>
    ///  简体转换为繁体
    /// </summary>
    /// <param name="source">源文本</param>
    /// <returns></returns>
    private async Task<string> ConvertToSimplifiedAsync(string source)
    {
        return await Task.FromResult(ChineseConverter.Convert(source, ChineseConversionDirection.TraditionalToSimplified));
    }

    /// <summary>
    ///   繁體轉換為簡體
    /// </summary>
    /// <param name="source">源文本</param>
    /// <returns></returns>
    private async Task<string> ConvertToTraditionalAsync(string source)
    {
        return await Task.FromResult(ChineseConverter.Convert(source, ChineseConversionDirection.SimplifiedToTraditional));
    }
}
